package com.roc.template.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisUtil {
//	private log log = logFactory.getlog(RedisUtil.class);
	
	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		try {
			for (String key : keys) {
		    	remove(key);
		    }
		}catch(Exception e) {
			log.error("批量删除缓存失败,value:"+keys);
			log.error("批量删除缓存失败",e);
		}
	}
	/**
	 * 批量删除key
	 *
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
	    try {
	    	Set<Serializable> keys = redisTemplate.keys(pattern);
		    if (keys.size() > 0)
		    redisTemplate.delete(keys);
		}catch(Exception e) {
			log.error("批量删除缓存失败,key:"+pattern);
			log.error("批量删除缓存失败",e);
		}
	}
	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
	    try {
	    	if (exists(key)) {
	    		redisTemplate.delete(key);
	    	}
		}catch(Exception e) {
			log.error("删除缓存失败,key:"+key);
			log.error("删除缓存失败",e);
		}
	}
	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
	    try {
	    	return redisTemplate.hasKey(key);
		}catch(Exception e) {
			log.error("判断缓存中是否有对应的value失败,key:"+key);
			log.error("判断缓存中是否有对应的value失败",e);
			return false;
		}
	}
	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public  String get(final String key) {
	    try {
		    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		    return (String) operations.get(key);
		}catch(Exception e) {
			log.error("读取缓存失败,key:"+key);
			log.error("读取缓存失败",e);
			return null;
		}
	}
	
	/**
	 * 批量读取缓存
	 * 
	 * @param keys
	 * @return
	 */
	public List<Object> mget(final Collection<String> keys) {
		try {
			ValueOperations<String, Object> operations = redisTemplate.opsForValue();
			return operations.multiGet(keys);
		}catch(Exception e) {
			log.error("批量读取缓存失败,key:"+keys);
			log.error("批量读取缓存失败",e);
			return null;
		}
	}
	
	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
	    try {
	    	ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		    operations.set(key, value);
		    return true;
		}catch(Exception e) {
			log.error("写入缓存失败,key:"+key+"|"+value);
			log.error("写入缓存失败",e);
			return false;
		}
	}


	/**
	 * 写入缓存--设置时长(单位秒)
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value, Long expireTime) {
	    try {
		    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		    operations.set(key, value);
		    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
		    return true;
	    } catch (Exception e) {
	    	log.error("限时写入缓存失败,key:"+key+"|"+value);
			log.error("限时写入缓存失败",e);
			return false;
	    }
    }
	
	/**
	 * 写入缓存--设置时长  value 自增
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public long incr(final String key, Long value, long expireTime) {
		long result = 0;
	    try {
		    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		    
		    if(expireTime>0) {
		    	if(! exists(key)) {
		    		result=operations.increment(key, value);
				    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
		    	}else {
		    		result=operations.increment(key, value);
		    	}
		    }else {
		    	result=operations.increment(key, value);
		    }
		    return result;
	    } catch (Exception e) {
	    	log.error("限时写入缓存失败,key:"+key+"|"+value);
			log.error("限时写入缓存失败",e);
	    }
		return result;
    }

	public boolean mset(final Map<String, Object> map) {
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.multiSet(map);
			return true;
		} catch (Exception e) {
			log.error("批量设置缓存失败",e);
			return false;
		}
	}

	/**
	 * 批量设置指定key的hash
	 * @param key
	 * @param value
	 * @return
	 */
    public boolean hmset(String key, Map<String, String> value) {
        try {
            redisTemplate.opsForHash().putAll(key, value);
            return true;
        } catch (Exception e) {
	        log.error("批量设置指定key的hash失败",e);
			return false;
        }
    }
    
    /**
     * 获取指定key的hash
     * @param key
     * @return
     */
    public  Map<String,String> hmget(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
        	log.error("获取指定key的hash失败",e);
			return null;
        }
    }
}